<?php
/**
 * @title 2477. 到达首都的最少油耗
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $roads
     * @param Integer $seats
     * @return Integer
     */
    function minimumFuelCost($roads, $seats) {
        /**
         * @since 2023-12-05 从首都城市（根节点）出发，层次遍历
         *
         *       0
         *      / \
         *     1   2
         *    / \
         *   3   4
         */

        /**
         * @since 2023-12-05 总耗油量
         */
        $totalFuelCost = 0;

        /**
         * @since 2023-12-05 哈希建立城市到城市之间关系，遍于查找
         */
        $cityToCityArray = [];
        foreach ($roads as list($city1, $city2)){
            $cityToCityArray[$city1][$city2] = $city2;
            $cityToCityArray[$city2][$city1] = $city1;
        }

        /**
         * @since 2023-12-05 每层的城市点
         */
        $levelCityArray = [];

        /**
         * @since 2023-12-05 回去的城市，上一层城市点，父节点
         */
        $backCityArray = [];

        /**
         * @since 2023-12-05 层次遍历
         */
        $cityArray = [0];
        while($cityArray) {
            /**
             * @since 2023-12-05 记录当前层城市点
             */
            $levelCityArray[] = $cityArray;

            /**
             * @since 2023-12-05 下次遍历的城市集合
             */
            $nextCityArray = [];

            /**
             * @since 2023-12-05 遍历当前城市集合
             */
            foreach ($cityArray as $city1) {
                /**
                 * @since 2023-12-05 这条路径走到终点
                 */
                if (empty($cityToCityArray[$city1])) {
                } else {
                    /**
                     * @since 2023-12-05 遍历当前层城市点
                     */
                    foreach ($cityToCityArray[$city1] as $city2) {
                        /**
                         * @since 2023-12-05 下一层的点
                         */
                        $nextCityArray[] = $city2;

                        /**
                         * @since 2023-12-05 记住回去的路，父节点
                         */
                        $backCityArray[$city2] = $city1;

                        /**
                         * @since 2023-12-05 规避回头路
                         */
                        unset($cityToCityArray[$city2][$city1]);
                    }
                }
            }

            /**
             * @since 2023-12-05 赋值下次遍历的城市集合
             */
            $cityArray = $nextCityArray;
        }
        // return $levelCityArray;
        // return $backCityArray;

        /**
         * @since 2023-12-05 初始化，每个城市1个旅客
         */
        $cityPeopleArray = array_fill(0, count($roads)+1, 1);

        /**
         * @since 2023-12-05 从最下层节点往上回溯
         */
        for($i=count($levelCityArray)-1; $i>0; $i--){
            // echo str_repeat(PHP_EOL, 2), "第", ($i+1), "层";

            /**
             * @since 2023-12-05 遍历当前层
             */
            foreach ($levelCityArray[$i] as $city){
                /**
                 * @since 2023-12-05 city2的旅客数
                 */
                $peopleNum = $cityPeopleArray[$city];
                // echo PHP_EOL, "city={$city}, people={$peopleNum}";

                /**
                 * @since 2023-12-05 检查一辆车是否满员
                 */
                if($peopleNum >= $seats){
                    /**
                     * @since 2023-12-05 需要几辆车
                     */
                    $carNum = intval($peopleNum/$seats);

                    /**
                     * @since 2023-12-05 $carNum到终点的耗油量
                     */
                    $totalFuelCost += $carNum*$i;

                    /**
                     * @since 2023-12-05 剩下多少人
                     */
                    $peopleNum = $peopleNum % $seats;

                    // echo "，满员，先走{$carNum}辆车，耗油", ($carNum*$i);
                } else {}

                /**
                 * @since 2023-12-05 如果有剩下的人，回溯上个城市点，耗油+1
                 */
                if($peopleNum > 0){
                    $totalFuelCost++;

                    /**
                     * @since 2023-12-05 剩下的人，到上一层节点
                     */
                    $cityPeopleArray[$backCityArray[$city]] += $peopleNum;

                    // echo "，还有{$peopleNum}人不满1辆车到", $backCityArray[$city], "，耗油+1";
                } else {}
            }
        }

        /**
         * @return
         */
        return $totalFuelCost;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.05_2.php
 */
$datas = [
    [[[0,1],[0,2],[0,3]], 5], // 3
    [[[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], 2], // 7
    [[[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], 1], // 10
    [[[0,1],[0,2],[1,3],[1,4]], 5], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minimumFuelCost(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
